﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using Infragistics.Win.UltraWinExplorerBar;
using POS.BusinessComponents;
using POS.UI.Mantenimientos;

[assembly: CLSCompliant(false)]
namespace POS.UI
{
    public partial class PrincipalForm : Form
    {
        #region Constructors

        public PrincipalForm()
        {
            InitializeComponent();

            LoadMenu();
            ConfigureCulture();
        }

        #endregion
        
        #region Declarations 

        //TODO: Replace with security logic
        private const string userId = "admin";

        // Opciones del menú
        private const string AdministracionEstacion = "m_adm_est";
        private const string AdministracionImpuesto = "m_adm_imp";
        private const string InventarioArticulo = "m_inv_art";
        private const string InventarioLinea = "m_inv_lin";
        private const string InventarioMarca = "m_inv_mar";
        private const string ComprasProveedor = "m_com_pro";
        private const string ComprasTipoProveedor = "m_com_tpr";
        
        #endregion

        #region Event Handlers

        private void menuPrincipalExplorerBar_ItemClick(object sender, ItemEventArgs e)
        {
            switch (e.Item.Key)
            {
                case AdministracionEstacion:
                    ShowScreen(new MantEstacionesForm());
                    break;

                case AdministracionImpuesto:
                    ShowScreen(new MantImpuestosForm());
                    break;

                case InventarioArticulo:
                    ShowScreen(new MantArticulosForm());
                    break;

                case InventarioLinea:
                    ShowScreen(new MantLineasForm());
                    break;

                case InventarioMarca:
                    ShowScreen(new MantMarcasForm());
                    break;

                case ComprasTipoProveedor:
                    ShowScreen(new MantTipoProveedorForm());
                    break;

                case ComprasProveedor:
                    ShowScreen(new MantProveedoresForm());
                    break;
            }
        }

        private void ultraTree1_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
        {
            var form = new ReportViewerForm {MdiParent = this};
            ActivateMdiChild(form);
            form.Show();
        }

        private void cascadaMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void horizontalMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void verticalMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        #endregion

        #region Methods: Non-Public

        private void LoadMenu()
        {
            HideExplorerBarItems();
            HideMenuStripItems();
            var menuStripItems = GetStripMenues();
            UltraExplorerBarItem menuItem;
            var menu = BCMenu.GetMenu(userId);
            foreach(var item in menu)
            {
                if (menuPrincipalExplorerBar.Groups.Exists(item.Codigo))
                    menuPrincipalExplorerBar.Groups[item.Codigo].Visible = true;
                else
                {
                    menuItem = GetExplorerBarItem(item.Codigo);
                    if (menuItem != null)
                        menuItem.Visible = true;
                }

                foreach (var menuStripItem in menuStripItems)
                {
                    if (menuStripItem.Name == item.Codigo)
                    {
                        menuStripItem.Visible = true;
                        menuStripItem.ImageIndex = 0;
                        break;
                    }
                }
            }
        }
        
        private static void ConfigureCulture()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        }

        private void HideExplorerBarItems()
        {
            foreach (var group in menuPrincipalExplorerBar.Groups)
            {
                foreach (var item in group.Items)
                    item.Visible = false;
                group.Visible = false;
            }
            
        }

        private void HideMenuStripItems()
        {   
            foreach (var item in GetStripMenues())
                item.Visible = false;
        }

        private List<ToolStripItem> GetStripMenues()
        {
            var menues = new List<ToolStripItem>();
            foreach (ToolStripItem item in menuStrip.Items)
                GetStripMenues(item, menues);
            
            return menues;
        }

        private static void GetStripMenues(ToolStripItem current, ICollection<ToolStripItem> menues)
        {
            if (current.Tag == null || (current.Tag != null && current.Tag.ToString() != "sys")) 
                menues.Add(current);

            if (current.GetType() == typeof(ToolStripMenuItem))
            {
                foreach(ToolStripItem menu in ((ToolStripMenuItem)current).DropDownItems)
                    GetStripMenues(menu, menues);
            }
        }
        
        private UltraExplorerBarItem GetExplorerBarItem(string key)
        {
            foreach (UltraExplorerBarGroup group in menuPrincipalExplorerBar.Groups)
            {
                if (group.Items.Exists(key))
                    return group.Items[key];
            }
            return null;
        }

        private void ShowScreen(Form form)
        {   
            form.MdiParent = this;
            ActivateMdiChild(form);
            form.Show();
            form.Activate();
        }

        #endregion
        
    }
}
